package com.example.useless;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class HomeActivity extends AppCompatActivity {
    private TextView mText;
    private Button mButton1;
    private Button mButton2;
    private Button mButton3;
    private Button mButton4;
    private Button mButton5;
    private Button mButton6;
    private ImageView mPhotoView;
    private ActivityResultLauncher mActivityResultLauncher;

    @Override
    protected void onCreate(@Nullable Bundle savedInstancestate) {
        super.onCreate(savedInstancestate);
        setContentView(R.layout.activity_home);
        mText = findViewById(R.id.text);
        mPhotoView = findViewById(R.id.photo);


        mText.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(HomeActivity.this, LoginAcitity.class);
                mActivityResultLauncher.launch(intent);
            }
        });
        onActivityResult();
        mPhotoView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//跳转到相机
                mActivityResultLauncher.launch(intent);
            }
        });
        onActivityResult();
    }

//    private void onActivityResult(){
//        mActivityResultLauncher = registerForActivityResult(
//                new ActivityResultContracts.StartActivityForResult(),
//                new ActivityResultCallback<ActivityResult>() {
//                    @Override
//                    public void onActivityResult(ActivityResult result) {
//                        int resultCode = result.getResultCode();
//                        if (resultCode == RESULT_OK) {
//                            Intent intent = result.getData();
//                            String userName = intent.getStringExtra("username");
//                            String password = intent.getStringExtra("password");
//                            String name = intent.getStringExtra("name");
//                            mText.setText("用户名:" + userName + "\n密码:" + password + "\n姓名:" + name);
//                        }
//                    }
//                });
//    }

    private void onActivityResult() {
        mActivityResultLauncher = registerForActivityResult(
        new ActivityResultContracts.StartActivityForResult(),
        new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                int resultcode = result.getResultCode();
                if (resultcode == RESULT_OK) {
                    Bitmap targetBitmap = result.getData().getParcelableExtra("data");//获取数据
                    mPhotoView.setImageBitmap(targetBitmap);
                }
            }
        });

    }
}

